package com.wiz.web.controllor;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.List;

import javax.annotation.Resource;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import sun.misc.BASE64Decoder;

import com.google.gson.reflect.TypeToken;
import com.wiz.common.util.ThreeDESUtil;
import com.wiz.web.dao.mapper.OilTankMapper;
import com.wiz.web.service.OilTankService;
import com.wiz.web.vo.criteria.FpCriteria;
import com.wiz.web.vo.criteria.ListCriteria;
import com.wiz.web.vo.criteria.OilTankCriteria;
import com.wiz.web.vo.to.StationLoginTO;

@SuppressWarnings("all")
@Controller
@RequestMapping("/oiltank")
public class OilTankMonitorControllor extends BaseControllor{
	private transient final Logger log = LoggerFactory.getLogger(getClass());
	
	@Resource
	private OilTankService oilTankService;
	@Resource
	private OilTankMapper oilTankMapper;

	/**
	 * 油站油罐激活
	 */
	@RequestMapping(value = "/activate", method = RequestMethod.POST)
	@ResponseBody
	public String activateOilTank(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		try{
			OilTankCriteria criteria = gson.fromJson(params, new TypeToken<OilTankCriteria>(){}.getType());
			return oilTankService.activateOilTank(criteria);
		}catch (Exception e) {
			e.printStackTrace();
			log.error("activateOilTank error in "+stationId+"  params="+params+"  exception="+e.toString(),e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	/**
	 * 油罐同步
	 */
	@RequestMapping(value = "/config", method = RequestMethod.POST)
	@ResponseBody
	public String config(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String monitor = null;
		try{
			monitor = getParams(stationId,params);
			ListCriteria<OilTankCriteria> listCriteria = ListCriteria.fromJson(monitor, OilTankCriteria.class);
			List<OilTankCriteria> criteriaList = listCriteria.getDataList();
			return oilTankService.configOilTank(stationId,criteriaList);
		}catch (Exception e) {
			e.printStackTrace();
			log.error("monitor error in "+stationId+"  params="+monitor+"  exception="+e.toString(),e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	/**
	 * 油罐同步
	 */
	@RequestMapping(value = "/monitor", method = RequestMethod.POST)
	@ResponseBody
	public String monitor(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String monitor = null;
		try{
			monitor = getParams(stationId,params);
			ListCriteria<OilTankCriteria> listCriteria = ListCriteria.fromJson(monitor, OilTankCriteria.class);
			List<OilTankCriteria> criteriaList = listCriteria.getDataList();
			return oilTankService.oilTankMonitor(stationId,criteriaList);
		}catch (Exception e) {
			e.printStackTrace();
			log.error("monitor error in "+stationId+"  params="+monitor+"  exception="+e.toString(),e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	/**
	 * 油站对应油品的油枪信息
	 */
	@RequestMapping(value = "/fp", method = RequestMethod.POST)
	@ResponseBody
	public String getFPByStationPR(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String monitor = null;
		try{
			monitor = getParams(stationId,params);
			FpCriteria criteria = gson.fromJson(monitor, new TypeToken<FpCriteria>(){}.getType());
			return oilTankService.getFPByStationPR(criteria);
		}catch (Exception e) {
			e.printStackTrace();
			log.error("getFPByStationPR error in "+stationId+"  params="+monitor+"  exception="+e.toString(),e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	@RequestMapping(value = "/tankfp", method = RequestMethod.POST)
	@ResponseBody
	public String tankFP(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String monitor = null;
		try{
			monitor = getParams(stationId,params);
			OilTankCriteria criteria = gson.fromJson(monitor, new TypeToken<OilTankCriteria>(){}.getType());
			return RESPONSE_CODE_OK;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("tankFP error in "+stationId+"  params="+monitor+"  exception="+e.toString(),e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	/**
	 * 站点油罐机器的mac
	 * @param stationId
	 * @return
	 * @throws IOException 
	 * @throws UnsupportedEncodingException 
	 */
	private byte[] getStationMac(String stationId) throws UnsupportedEncodingException, IOException {
		Object o = redisManager.get(String.format(STATION_OIL_TANK_MAC, stationId));
		String mac = null;
		if(o == null){
			StationLoginTO loginTo = oilTankMapper.getStationMac(stationId);
			redisManager.set(String.format(STATION_OIL_TANK_MAC, stationId), loginTo.getStationMac());
			mac = loginTo.getStationMac();
		}else{
			mac = o.toString();
		}
		return new BASE64Decoder().decodeBuffer(ThreeDESUtil.buildKey(mac.getBytes()));
	}
	
	/**
	 * 解密参数信息
	 * @param stationId
	 * @param paramStr
	 * @return
	 * @throws Exception 
	 * @throws BadPaddingException 
	 * @throws IllegalBlockSizeException 
	 * @throws InvalidAlgorithmParameterException 
	 * @throws InvalidKeySpecException 
	 * @throws NoSuchPaddingException 
	 * @throws NoSuchAlgorithmException 
	 * @throws InvalidKeyException 
	 * @throws IOException 
	 */
	private String getParams(String stationId, String params) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException, 
	InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, IOException{
		return new String(ThreeDESUtil.des3DecodeCBC(getStationMac(stationId), 
				keyiv, new BASE64Decoder().decodeBuffer(params)),"UTF-8");
//		return params;
	}
}
